/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sadhar.sipp.itembiaya;

import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassSession;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.perekamananggarankegiatanunit.ItemBiayaHargaItem;
import org.sadhar.sipp.perekamananggarankegiatanunit.PerekamanAnggaranKegiatanUnitDAO;
import org.sadhar.sipp.perekamananggarankegiatanunit.PerekamanAnggaranKegiatanUnitDAOImpl;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class ItemBiayaPopupWnd extends ClassApplicationModule{
    private Textbox txtboxKeySearch;
    private Textbox txtboxKodePosBiaya;
    private Listbox lstboxItemBiaya;
    private ItemBiayaDAO itemDAO;
    private ClassSession sesi = new ClassSession("2009", "02001000");
    private PerekamanAnggaranKegiatanUnitDAO unitDAO;

    public ItemBiayaPopupWnd(){
        itemDAO = new ItemBiayaDAOImpl();
        unitDAO = new PerekamanAnggaranKegiatanUnitDAOImpl();
    }

    public void onCreate()throws Exception{
        txtboxKeySearch = (Textbox)getFellow("txtboxKeySearch");
        lstboxItemBiaya = (Listbox)getFellow("lstboxItemBiaya");
        txtboxKodePosBiaya = (Textbox)getFellow("txtboxKodePosBiaya");
        loadItemBiaya();
    }

    private void loadItemBiaya()throws Exception{
        lstboxItemBiaya.getItems().clear();
        for(ItemBiaya obj : itemDAO.list(Integer.valueOf(txtboxKodePosBiaya.getValue()),txtboxKeySearch.getValue())){
            Listitem item = new Listitem();
            item.setValue(obj);
            item.appendChild(new Listcell(obj.getNamaItem()));
            lstboxItemBiaya.appendChild(item);
        }
    }

    public void lstboxItemBiayaOnSelect()throws Exception{
        ItemBiaya item = (ItemBiaya)lstboxItemBiaya.getSelectedItem().getValue();
        ItemBiayaHargaItem hargaItem = unitDAO.selectHargaItem(sesi.getThnAnggaran(), item.getKodeItem());
        Window window = (Window) this.getRoot().getFellow("contentDiv").getFirstChild();
        Label lblItemBiaya = (Label)window.getFellow("lblItemBiaya");
        lblItemBiaya.setValue(hargaItem.getNamaItem());
        Label lblSatuan = (Label)window.getFellow("lblSatuan");
        lblSatuan.setValue(hargaItem.getSatuan());
        Doublebox txtboxHarga = (Doublebox)window.getFellow("txtboxHarga");
        Textbox txtboxKodeItemBiayaOrIdHargaBarang = (Textbox)window.getFellow("txtboxKodeItemBiayaOrIdHargaBarang");
        txtboxKodeItemBiayaOrIdHargaBarang.setValue(item.getKodeItem()+"");
        if(hargaItem.getHarga()>0){
            txtboxHarga.setValue(ClassAntiNull.AntiNullDouble(hargaItem.getHarga()));
            txtboxHarga.setDisabled(true);
        }else{
            txtboxHarga.setDisabled(false);
        }
        this.detach();
    }
}
